﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Basic4Net.Utility.Excel
{
	public class ExcelConnectionString
	{
        #region 内部变量
        private String _FileFullName;
        private String FileFullName
        {
            get { return _FileFullName; }
            set { _FileFullName = value; }
        }
		private HDR? hdr;
		private IMEX? imex;
		#endregion

		#region 构造函数
		/// <summary>
		/// 用于获取连接Excel的字符串的类。
		/// </summary>
		/// <param name="FileFullName">文件名，包含盘符扩展名。</param>
		/// <param name="hdr">是否包含列名</param>
		/// <param name="imex">读取模式</param>
		public ExcelConnectionString(String FileFullName, HDR? hdr, IMEX? imex)
		{
			this.FileFullName = FileFullName;
			this.hdr = hdr;
			this.imex = imex;
		}

		/// <summary>
		/// 读取时全部转换为文本。
		/// </summary>
		/// <param name="FileFullName">文件名，包含盘符扩展名。</param>
		/// <param name="hdr">是否包含列名</param>
		public ExcelConnectionString(String FileFullName, HDR? hdr)
			: this(FileFullName, hdr, IMEX.IntermixedMode)
		{ }

		/// <summary>
		/// Excel文件包含列名。
		/// </summary>
		/// <param name="FileFullName">文件名，包含盘符扩展名。</param>
		/// <param name="imex">读取模式</param>
		public ExcelConnectionString(String FileFullName, IMEX? imex)
			: this(FileFullName, HDR.Yes, imex)
		{ }

		/// <summary>
		/// Excel文件包含列名，读取时全部转换为文本。
		/// </summary>
		/// <param name="FileFullName">文件名，包含盘符扩展名。</param>
		public ExcelConnectionString(String FileFullName)
			: this(FileFullName, HDR.Yes, IMEX.IntermixedMode)
		{ }
		#endregion

		#region 内部方法
		/// <summary>
		/// 获取HDR的连接串文本。
		/// </summary>
		/// <param name="hdr"></param>
		/// <returns></returns>
		private String GetHDRString(HDR? hdr)
		{
			String HDRString = String.Empty;
			switch (hdr)
			{
				case HDR.No:
					HDRString = ";HDR=No";
					break;
				case HDR.Yes:
					HDRString = ";HDR=Yes";
					break;
				default:
					break;
			}
			return HDRString;
		}

		/// <summary>
		/// 获取IMEX的连接串文本。
		/// </summary>
		/// <param name="imex"></param>
		/// <returns></returns>
		private String GetIMEXString(IMEX? imex)
		{
			String IMEXString = String.Empty;
			switch (imex)
			{
				case IMEX.UnknownMode0:
					IMEXString = ";IMEX=0";
					break;
				case IMEX.IntermixedMode:
					IMEXString = ";IMEX=1";
					break;
				case IMEX.UnknownMode2:
					IMEXString = ";IMEX=2";
					break;
				default:
					break;
			}
			return IMEXString;
		}
		#endregion

		#region 公共方法
		public String ConnectionString()
		{
			String ConnectionString = String.Empty;
			String FillExtension = (FileFullName.Substring(FileFullName.LastIndexOf('.'), FileFullName.Length - FileFullName.LastIndexOf('.'))).ToUpper();

			switch (FillExtension)
			{
				case ".XLS":
					ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0{1}{2}\";";
					break;
				case ".XLSX":
					ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml{1}{2}\";";
					break;
				case ".XLSB":
					ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0{1}\";";
					break;
				case ".XLSM":
					ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro{1}\";";
					break;
				default:
					break;
			}
			ConnectionString = String.Format(ConnectionString, this.FileFullName, GetHDRString(this.hdr), GetIMEXString(this.imex));

			return ConnectionString;
		}
		#endregion
	}
}
